package com.cynteka.cyfoman.core.collection;

import com.cynteka.cyfoman.core.ValueObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: Sergey
 * Date: 05.08.11
 * Time: 11:25
 */
public class RegexpTableCollection extends BaseCollection<ValueObject<String, String>> implements ICollection<ValueObject<String, String>> {

    public static final String CONTENT_DEFAULT_PARAM_NAME = "content";
    public static final String DEFAULT_COLLECTION_ID = "regexpTable";

    private String contentParamName = CONTENT_DEFAULT_PARAM_NAME;
    private String regexp;
    private Pattern pattern;
    private List<String> variables;

    public RegexpTableCollection() {
        super(DEFAULT_COLLECTION_ID);
    }

    public RegexpTableCollection(String collectionId, String regexp, List<String> variables) {
        super(collectionId);
        this.regexp = regexp;
        this.variables = variables;
    }

    public String getContentParamName() {
        return contentParamName;
    }

    public void setContentParamName(String contentParamName) {
        this.contentParamName = contentParamName;
    }

    public String getRegexp() {
        return regexp;
    }

    public void setRegexp(String regexp) {
        this.regexp = regexp;
    }

    public List<String> getVariables() {
        return variables;
    }

    public void setVariables(List<String> variables) {
        this.variables = variables;
    }

    @Override
    public Collection<ValueObject<String, String>> getItems(Map<String, Object> params) {
        initRegexp();
        List<ValueObject<String, String>> res = new ArrayList<ValueObject<String, String>>();
        if(params != null) {
            Object oContent = params.get(contentParamName);
            if(oContent != null) {
                String content = oContent.toString();
                Matcher matcher = pattern.matcher(content);
                while(matcher.find()) {
                    ValueObject<String, String> valueObject = new ValueObject();
                    int groupIdx = 1;
                    for(String variable : variables) {
                        if(groupIdx <= matcher.groupCount()) {
                        String value = matcher.group(groupIdx);
                        // System.out.println(groupIdx + ":" + value);
                        if(variable != null && variable.trim().length() > 0) {
                            valueObject.setParam(variable, value);
                        }
                        }
                        groupIdx++;
                    }
                    res.add(valueObject);
                }
            }
        }
        return res;
    }

    protected void initRegexp() {
        if(pattern == null) {
            pattern = Pattern.compile(regexp);
        }
    }
}
